﻿$(document).ready(function () {
    $('.placeholderMenuItem_Clicked').removeClass('placeholderMenuItem_Clicked');
    $('#menuComplaints').attr('class', 'placeholderMenuItem_Clicked');
});
function fnRegisterComplaint() {
    var msg = '';
    var opts = { 'AlertText': msg, 'OKTxt': 'With User ID', 'CancelTxt': 'Without User ID', 'okClick': function () { window.location.href = "../../pages/complaints/regComplaintWithID.aspx"; }, 'cancelClick': function () { window.location.href = "../../pages/complaints/regComplaint.aspx"; }, 'reqCancel': true };
    RegComplaintAlert(opts);
}

function RegComplaintAlert(_opts) {
    var genPopupContainer = $("<div id='genricAlertPopup' class='TotPopUp'></div>");

    var genPopupHead = $("<div class='TopPopHead'></div>");
    var popHeadText = $("<span class='PopAletTxt'></span>");
    $(popHeadText).text("Register new complaint");
    $(genPopupHead).append(popHeadText);

    var popBodyContainer = $("<div class='BtmPopUoDiv'></div>");
    var popBodyAlertImg = $("<div class='AlertLatestImg'></div>");
    var popBodyAlertMsg = $("<span class='AlertConformMsg'></span>");
    $(popBodyAlertMsg).text(_opts.AlertText);
    var popBodyBtnWrap = $("<div class='ButtonsWrap'></div>");
    var popBodyBtnOK = $("<div id='btnOk' class='adminColorButton LeftBtnNewWrap' tabindex='1'></div>");
    $(popBodyBtnOK).text(_opts.OKTxt);

    var popBodyBtnCancel = $("<div id='btnCancel' class='adminGrayButton' tabindex='2'></div>");
    $(popBodyBtnCancel).text(_opts.CancelTxt);

    $(popBodyBtnWrap).append(popBodyBtnOK);
    if (_opts.reqCancel == true) {
        $(popBodyBtnWrap).append(popBodyBtnCancel);
    }
    else {
        $(popBodyBtnOK).css('margin-left', '50px');
    }
    $(popBodyContainer).append(popBodyAlertImg);
    $(popBodyContainer).append(popBodyAlertMsg);
    $(popBodyContainer).append(popBodyBtnWrap);

    $(genPopupContainer).append(genPopupHead);
    $(genPopupContainer).append(popBodyContainer);
    var tabCount = 0;
    $(genPopupContainer).dialog({
        height: 200,
        modal: true,
        width: 440,
        draggable: false,
        resizable: false,
        closeOnEscape: true,
        open: function () {
            $('body').find(".ui-dialog").focus();
            /*Custom code of tab index - by Dipin Kumar*/
            if (tabCount == 0) {
                $('#btnCancel').focus();
                $('#btnOk').blur();
            }
            else {
                $('#btnOk').focus();
                $('#btnCancel').blur();
            }
            $(genPopupContainer).keydown(function (e) {
                //alert(e.which);
                if (e.which == '9') {
                    if (tabCount == 0) {
                        //alert(tabCount);
                        $('#btnCancel').focus();
                        $('#btnOk').blur();
                        tabCount = 1;
                    }
                    else if (tabCount == 1) {
                        //alert(tabCount);
                        $('#btnOk').focus();
                        $('#btnCancel').blur();
                        tabCount = 0;
                    }
                }
                if (e.which == '13') {
                    if (tabCount == 0) {
                        //alert(tabCount + " Cancel");
                        $(popBodyBtnCancel).click();
                    }
                    else if (tabCount == 1) {
                        //alert(tabCount + " Ok");
                        $(popBodyBtnOK).click();
                    }
                }
            });
            //shortcut.add("y", function () { $(popBodyBtnOK).click(); });
            //shortcut.add("n", function () { $(popBodyBtnCancel).click(); });
            $(this).parents(".ui-dialog:first").find(".ui-dialog-titlebar-close").remove();
            $(this).parents(".ui-dialog:first").find(".ui-dialog-titlebar").remove();
            $(this).parents(".ui-dialog:first").removeClass("ui-widget-content").addClass("ui-widget-content_alt")
            $(popBodyBtnOK).click(function () {
                if (typeof (_opts.okClick) == "function") {
                    if (_opts.optsToOkFn != null) {
                        //alert(_opts.optsToOkFn);
                        _opts.okClick(_opts.optsToOkFn);

                    }
                    else {
                        _opts.okClick();
                    }
                    $('body').find("#genricAlertPopup").remove();
                }
                //shortcut.remove("y");
                //shortcut.remove("n");
            });
            $(popBodyBtnCancel).click(function () {
                if (typeof (_opts.cancelClick) == "function") {
                    _opts.cancelClick();
                    $('body').find("#genricAlertPopup").remove();
                }
                //shortcut.remove("y");
                //shortcut.remove("n");
            });
        },
        close: function () { $('body').find("#genricAlertPopup").remove(); }
    });
}
var isValidated = false;
function regComplaintWithoutID() {
    
    
    var issueid = genDynmID();
    //alert("regComplaintWithoutID");
     
    var cusName = $('#txtCustomerName').val();
    if (cusName.length == 0) {
        $('#cusNameAlert').text("**Please enter the customer name");
        return false;
    }
    var cusAddress = $('#txtCusAddress').val();
    if (cusAddress.length == 0) {
        $('#cusAddressAlert').text("**Please enter the customer address");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var cusRoute = $('#txtRoute').val();
    if (cusRoute.length == 0) {
        $('#cusRouteAlert').text("**Please enter the route info");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var cusLocation = $('#txtLocation').val();
    if (cusLocation.length == 0) {
        $('#cusLocationAlert').text("**Please enter the customer location");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var cusLandmark = $('#txtLandmark').val();
    if (cusLandmark.length == 0) {
        $('#cusLandmarkAlert').text("**Please enter any near by landmark");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var cusState = $('#txtState').val();
    var cusPostcode = $('#txtPostcode').val();
    var cusMobile = $('#txtMobile').val();
    if (cusMobile.length == 0) {
        $('#cusMobileAlert').text("**Please enter the customer mobile number");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var cusLandline = $('#txtLandline').val();
    var cusEmail = $('#txtEmail').val();

    var prodCate = $('select[id*=ddlProdCate] option:selected').text();
    var prodName = $('#txtProductName').val();
    if (prodName.length == 0) {
        $('#prodNameAlert').text("**Please enter the product name");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var prodModal = $('#txtModalNo').val();
    var prodCompressor = $('#txtCompressorNo').val();
    var prodIndoor = $('#txtIndoorNo').val();
    var prodOutdoor = $('#txtOutdoorNo').val();

    var compDesc = $('#txtAreaComplaints').val();
    if (compDesc.length == 0) {
        $('#compDescAlert').text("**Please enter the customer complaint on product");
        return false;
    }
//    else {
//        isValidated = true;
//    }
    var technician = $('select[id*=ddlTechnicians] option:selected').text();
    var scheduleDt = $('#txtScheduleDt').val();
    var status = $('select[id*=ddlSelectStatus] option:selected').text();
    var isSpare = $('input:radio[name=spare]:checked').val();
    var spareDetails = $('#txtSpareDetails').val();
    var _params = {
        'case':'insert',
        'cname': cusName,
        'caddress': cusAddress,
        'croute': cusRoute,
        'clocation': cusLocation,
        'clandmark': cusLandmark,
        'cstate': cusState,
        'cpostcode': cusPostcode,
        'cmobile': cusMobile,
        'clandline': cusLandline,
        'cemail': cusEmail,
        'pcate': prodCate,
        'pname': prodName,
        'pmodal': prodModal,
        'pcomp': prodCompressor,
        'pindoor': prodIndoor,
        'poutdoor': prodOutdoor,
        'compdesc': compDesc,
        'technician': technician,
        'scheduledt': scheduleDt,
        'status': status,
        'isspare': isSpare,
        'sparedetails': spareDetails,
        'issueid': issueid,
        'cusid': ''
    }
    var containerDIV = $('#addCustomerForm');
    var opts = { 'isLoadingGif': false, 'container': containerDIV };
    MakeJAjaxCall("../../_services/manageComplaints.ashx", _params, 'html', fnRegComplaintOnSuccess, '', '', opts);

}

function fnRegComplaintOnSuccess(response) {
    if (response == "y") {
        window.location.href = "../../pages/complaints/Default.aspx";

    }
}

function regComplaintWithID() {
    alert("regComplaintWithID");
}
function fnChangeComplaintStatus(obj) {
    var selectedIndex = $('#ddlSelectStatus option:selected').val();
    if (selectedIndex == 1) {
        $('#divIsSpare').show();
    }
    else {
        $('#divIsSpare').hide();
        $('#spareDetails').hide();
    }
}
function fnClickYes() {
    $('#spareDetails').show();
}
function fnClickNo() {
    $('#spareDetails').hide();
}
function fnDdlCateChange(obj) {
    //alert($('select[id*=ddlProdCate] option:selected').text());
    if ($('select[id*=ddlProdCate] option:selected').text() != "Air Conditioner") {
        $('#txtCompressorNo').attr('disabled', 'disabled');
        $('#txtCompressorNo').attr('style', 'background-color:#F1F1F1;');
        $('#txtIndoorNo').attr('disabled', 'disabled');
        $('#txtIndoorNo').attr('style', 'background-color:#F1F1F1;');
        $('#txtOutdoorNo').attr('disabled', 'disabled');
        $('#txtOutdoorNo').attr('style', 'background-color:#F1F1F1;');
    }
    else {
        $('#txtCompressorNo').removeAttr('disabled');
        $('#txtCompressorNo').attr('style', 'background-color:#E8E8E8;');
        $('#txtIndoorNo').removeAttr('disabled');
        $('#txtIndoorNo').attr('style', 'background-color:#E8E8E8;');
        $('#txtOutdoorNo').removeAttr('disabled');
        $('#txtOutdoorNo').attr('style', 'background-color:#E8E8E8;');
    }
}
function fnGetCustomerData() {
    var cusId = $('#txtCustomerID').val();
    if (cusId.length==0) {
        $('#cusIDAlert').text("**Please enter the customer ID");
        return false;
    }
    var _params = { 'case': 'getCusData', 'cusid': cusId }
    var containerDIV = $('#addCustomerForm');
    var opts = { 'isLoadingGif': false, 'container': containerDIV };
    MakeJAjaxCall("../../_services/manageComplaints.ashx", _params, 'html', getCustomerDataOnSuccess, '', '', opts);
}
function getCustomerDataOnSuccess(response) {
    var data = $.parseJSON(response);
    if (data.length > 0) {
        $('#txtCustomerName').val(data[0].name);
        $('#txtCusAddress').val(data[0].address);
        $('#txtLocation').val(data[0].city);
        $('#txtState').val(data[0].state);
        $('#txtPostcode').val(data[0].postcode);
        $('#txtMobile').val(data[0].mobile);
        $('#txtLandline').val(data[0].landline);
        $('#txtEmail').val(data[0].email);
    }
    else {
        var msg = 'Customer ID does not exist/Invalid customer ID';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}
function printComplaints() {
    var display_setting = "toolbar=no,location=no,directories=no,menubar=no,";
    display_setting += "scrollbars=no,width=700, height=600, left=100, top=25";

    var content_innerhtml = document.getElementById("PrintComplaints").innerHTML;
    var document_print = window.open("", "", display_setting);
    document_print.document.open();
    document_print.document.write('<html><head><title>Print AMC Acknowledge</title></head>');
    document_print.document.write('<body style="font-family:verdana; font-size:12px;" >');
    document_print.document.write(content_innerhtml);
    document_print.document.write('</body></html>');
    document_print.print();
    document_print.document.close();
    return false;
}
function fnViewComplaintDetails(obj) {
    var compid = $(obj).attr('rowid');
    //alert(compid);
    $('#popComplaintContainer').dialog({
        modal: true,
        height: 580,
        width: 750,
        open: function () {
            $('#ui-dialog-title-popComplaintContainer').text("Complaint Log");
            //$('#txtAreaComments').empty();
            var parentDiv = $('#popComplaintContainer');
            var moduleName = "~/_modules/complaints/ucComplaintDetails.ascx";
            var _params = '&comid=' + compid;
            var opts = { 'onsucess': '', 'params': _params };
            loadModuler(moduleName, parentDiv, opts);
        }

    });
}
var gCompId = null;
function fnUpdateComplaintDetails(obj) {
    var compid = $(obj).attr('rowid');
    gCompId = $(obj).attr('rowid');
    $('#popComplaintContainer').dialog({
        modal: true,
        height: 580,
        width: 750,
        open: function () {
            $('#ui-dialog-title-popComplaintContainer').text("Update Complaints");
            //$('#txtAreaComments').empty();
            var parentDiv = $('#popComplaintContainer');
            var moduleName = "~/_modules/complaints/ucUpdateComplaint.ascx";
            var _params = '&comid=' + compid;
            var opts = { 'onsucess': '', 'params': _params };
            loadModuler(moduleName, parentDiv, opts);
        },
        close: function () { gCompId = null; }

    });
}
function fnUpdateComplaint() {
    var compId = gCompId;
    if (gCompId != null) {
        var compDesc = $('#txtAreaComplaints').val();
        if (compDesc.length == 0) {
            $('#compDescAlert').text("**Please update the current complaint status");
            return false;
        }
        var technician = $('select[id*=ddlTechnicians] option:selected').text();
        var scheduleDt = $('#txtScheduleDt').val();
        var status = $('select[id*=ddlSelectStatus] option:selected').text();
        var fixed = 0;
        if ($('select[id*=ddlSelectStatus] option:selected').val() == 2) {
            fixed = 1;
        }
        else {
            fixed = 0;
        }
        var isSpare = $('input:radio[name=spare]:checked').val();
        var spareDetails = $('#txtSpareDetails').val();
        var _params = {
            'case': 'update',
            'compdesc': compDesc,
            'technician': technician,
            'scheduledt': scheduleDt,
            'status': status,
            'isspare': isSpare,
            'sparedetails': spareDetails,
            'compid':compId,
            'isfix':fixed
        }
        if ($('select[id*=ddlSelectStatus] option:selected').val() == 2 && $('input[type=hidden][id*=hidIsSpare]').val() == 1) {
            var msg = 'Customer provided with a spare component, please remember to collect it back';
            var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () {
                $('body').find("#genricAlertPopup").remove();
                var containerDIV = $('#addCustomerForm');
                var opts = { 'isLoadingGif': false, 'container': containerDIV };
                MakeJAjaxCall("../../_services/manageComplaints.ashx", _params, 'html', fnUpdateComplaintSuccess, '', '', opts);
            }, 'cancelClick': '', 'reqCancel': false
            };
            genAlertPOPUP(opts);
        }
        else {
            var containerDIV = $('#addCustomerForm');
            var opts = { 'isLoadingGif': false, 'container': containerDIV };
            MakeJAjaxCall("../../_services/manageComplaints.ashx", _params, 'html', fnUpdateComplaintSuccess, '', '', opts);
        }
    }
}
function fnUpdateComplaintSuccess(response) {
    if (response == "y") {
        $('#popComplaintContainer').dialog('close');
        window.location.href = "../../pages/complaints/Default.aspx";
    }
    else {
        var msg = 'Could not update complaint. Please try later';
        var opts = { 'AlertText': msg, 'OKTxt': 'OK', 'CancelTxt': 'No', 'okClick': function () { $('#popComplaintContainer').dialog('close'); $('body').find("#genricAlertPopup").remove(); }, 'cancelClick': '', 'reqCancel': false };
        genAlertPOPUP(opts);
    }
}